﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class FContactWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.FContactWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("Title", "CDSP-Góp ý");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("Defaul",GetText("Defaul","Kiểu ngầm định"),1),                   
                };
            }
        }



        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "FContactWP";
            }
        }

        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("Defaul"))
                {
                    return ClientRenderWebPartDefaul(TabModuleId, HasPorlet);
                }

                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }



        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "F93352FB-3159-4498-85ED-B35B7C1AE211","Gửi ý kiến Website"
            });
            return dtResult;
        }


        private string ClientRenderWebPartDefaul(string TabModuleId, string HasPorlet)
        {
            try
            {
                TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
                if (TabModuleCls == null) throw new Exception("[" + TabModuleId + "] " + GetText("TabModuleIdNull", "not found!"));
                string Html =
                    WebPartUtilities.AttachJavascript(WebPartFolder, "FSuggest.js") +

                    "<div id=\"leftsub\">" +
                        "<div class=\"leftsubHeader\">" +
                        "<h3>Liên hệ</h3>"+                            
                        "<div class=\"clrfix\"></div>" +
                        "</div>" +                    
                    "<div id=contentholder>" +
                    "<div class = divsuggest>" +
                    "<p>Gửi một email tới đầu mối liên lạc này:</p><br/>"+                      
                        //"<form name=abc action=Default.aspx>" +
                        "<table class = tablesuggest>" +
                        "<tr><td>" + GetText("FullName", "Họ và tên") + "<span class = \"attention\">(*)</span></td>" +
                        "<td><input type=text id=txt_name /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td >" + GetText("Email", "Địa chỉ Email") + "<span class = attention>(*)</span></td>" +
                        "<td><input type=text id=txt_email /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td >" + GetText("Phone", "Điện thoại") + "</td>" +
                        "<td><input type=text id=txt_phone /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td>" + GetText("Address", "Địa chỉ") + "</td>" +
                        "<td><input type=text id=txt_address /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td >" + GetText("Subject", "Tiêu đề") + "<span class = \"attention\">(*)</span></td>" +
                        "<td><input type=text id=txt_subject /></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td class = \"SuggestContent\">" + GetText("SuggestContent", "Nội dung góp ý") + "<span class = attention>(*)</span></td>" +
                        "<td><textarea id=txt_content1  rows = \"6\" cols = \"60\"></textarea> </td></tr>" +
                        "<tr><td></td>" +
                        "<td><img src=/vsi_portlets/captcha></td>" +
                        "</tr>" +
                        "<tr>" +
                        "<td></td>" +
                        "<td>" +
                        "<input type=\"button\" value=\"" + GetText("SuggestSent", "Gửi") + "\" onclick =\"javascript:checkForm('" + TabModuleCls.belongSite + "');\" />&nbsp;" +
                        "<input type=reset value=\"" + GetText("SuggestReset", "Nhập lại") + "\" />&nbsp;" +
                        "</td></tr>" +
                        "<tr><td></td>" +
                        "<td><span class =\"attention\">(*)</span>" + GetText("Requirement", "là yêu cầu bắt buộc.") + "</td></tr>" +
                        "</table>" +
                        //"</form>" +
                    "</div>"+
                    "</div>"+
                    "</div>";
                return Html;
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod()]
        public string ServerSideInsertSuggest(string Subject, string Content, string Email, string Name, string Phone, string Address, string belongSite)
        {
            try
            {
                Name = Name.Trim();
                Subject = Subject.Trim();
                Content = Content.Trim();
                if (Name.Length == 0 || Subject.Length == 0 || Content.Length == 0)
                {
                    return GetText("Warning", "Yêu cầu nhập thông tin góp ý!");
                }
                else
                {
                    string Red = new ContactBLL().InsertSuggest(null, Subject, Content, Email, Name, Address, Phone, belongSite);
                    return GetText("OkSentSuggest", "Gửi góp ý thành công,xin cảm ơn bạn!");
                }
            }
            catch (Exception ex)
            {
                return "ERROR:" + ex.Message;
            }
        }

        public override void RegAjaxPro(System.Web.UI.Page ActivePage)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(FContactWP), ActivePage);
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
    }
}
